﻿using Fuse8.ContentManagementFramework.Domain;
using Fuse8.ContentManagementFramework.Domain.Commands;
using Fuse8.DomainFramework.Common.Locator;
using Fuse8.DomainFramework.DataAccess;
using Fuse8.DomainFramework.DataAccess.SqlDatabase;
using System.Collections.Generic;
using System.Linq;

namespace Fuse8.ContentManagementFramework.DataAccess.SqlAccess.Commands
{
    public class CmsMediaFileSelectByFolderIdsDataCommand : CmsMediaFileSelectByFolderIdsCommand
    {
        public override IQueryable<CmsMediaFile> Execute()
        {
            IEnumerable<CmsMediaFile> result = null;

            var factory = DependencyServiceLocator.Current.GetInstance<IUnitOfWorkFactory<CmsMediaFile>>();

            var currentUnitOfWork = factory.Get(false) as SqlUnitOfWork<CmsMediaFile>;

            if (currentUnitOfWork != null)
            {
                var mapper = DependencyServiceLocator.Current.GetInstance<ISqlEntityDataMapper<CmsMediaFile, int>>();

                result = SqlUtility<CmsMediaFile, int>.ExecuteReader(
                        currentUnitOfWork.Connection,
                        currentUnitOfWork.Transaction,
                        "cms_sp_MediaFileSelectByFolderIds",
                        new
                        {
                            Ids = CommandParameter.Ids
                        },
                        mapper);
            }

            return result != null ?
                result.AsQueryable() :
                new List<CmsMediaFile>().AsQueryable();
        }
    }
}